using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EditDeploymentSettings.Properties;

namespace EditDeploymentSettings
{
    public partial class frmEditDeploymentSettings : Form
    {
        private DeploymentEnvironments _environments = new DeploymentEnvironments();

        public frmEditDeploymentSettings()
        {
            InitializeComponent();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Settings.Default.RecentFolder) &&
                System.IO.Directory.Exists(Settings.Default.RecentFolder))
            {
                folderBrowserDialog1.SelectedPath = Settings.Default.RecentFolder;
            }
            else
            {
                //folderBrowserDialog1.SelectedPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                folderBrowserDialog1.SelectedPath = @"C:\";
            }

            DialogResult result = folderBrowserDialog1.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                LoadEnvironments(folderBrowserDialog1.SelectedPath);
            }
        }

        private void LoadEnvironments(string folderName)
        {
            _environments.Load(folderName);

            dataGridView1.DataSource = _environments.SettingsData;

            dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.BlanchedAlmond;
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }

            Settings.Default.RecentFolder = folderName;
            Settings.Default.Save();

            Text = "Edit Deployment Settings - " + folderName;
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _environments.Save((DataTable)dataGridView1.DataSource);
        }

        private void frmEditDeploymentSettings_Load(object sender, EventArgs e)
        {
            // Set window location
            if (Settings.Default.WindowLocation != null)
            {
                this.Location = Settings.Default.WindowLocation;
            }

            // Set window size
            if (Settings.Default.WindowSize != null)
            {
                this.Size = Settings.Default.WindowSize;
            }

            if (!string.IsNullOrEmpty(Settings.Default.RecentFolder) &&
                System.IO.Directory.Exists(Settings.Default.RecentFolder))
            {
                LoadEnvironments(Settings.Default.RecentFolder);
            }
        }

        private void frmEditDeploymentSettings_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Copy window location to app settings
            Settings.Default.WindowLocation = this.Location;

            // Copy window size to app settings
            if (this.WindowState == FormWindowState.Normal)
            {
                Settings.Default.WindowSize = this.Size;
            }
            else
            {
                Settings.Default.WindowSize = this.RestoreBounds.Size;
            }

            // Save settings
            Settings.Default.Save();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox aboutBox = new AboutBox();
            aboutBox.ShowDialog(this);
        }

        private void findToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClearMarks();
            FindForm find = new FindForm();
            if (find.ShowDialog(this) == DialogResult.OK)
            {
                bool matchCase = find.chkMatchCase.Checked;
                for (int c=0; c<dataGridView1.ColumnCount; c++)
                {
                    for (int r=0; r<dataGridView1.RowCount-1; r++)
                    {
                        DataGridViewCell cell = dataGridView1[c, r];
                        string val = cell.Value as string;
                        if (!string.IsNullOrEmpty(val))
                        {
                            int index = -1;
                            if (matchCase)
                                index = val.IndexOf(find.txtFind.Text);
                            else
                                index = val.IndexOf(find.txtFind.Text, StringComparison.OrdinalIgnoreCase);

                            if (index > -1)
                            {
                                cell.Style.BackColor = Color.Cyan;
                            }
                        }
                    }
                }
            }
        }

        private void clearMarksToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClearMarks();
        }

        private void ClearMarks()
        {
            for (int c = 0; c < dataGridView1.ColumnCount; c++)
            {
                for (int r = 0; r < dataGridView1.RowCount - 1; r++)
                {
                    DataGridViewCell cell = dataGridView1[c, r];

                    if (0 == c)
                        cell.Style.BackColor = Color.BlanchedAlmond;
                    else
                        cell.Style.BackColor = Color.White;
                }
            }

        }
	}
}